<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}订单信息`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="drawerProps.row!.orderNo" clearable></el-input>
      </el-form-item>
      <el-form-item label="金额" prop="amount">
        <el-input v-model="drawerProps.row!.amount" clearable></el-input>
      </el-form-item>
      <el-form-item label="收货人" prop="receiver">
        <el-input v-model="drawerProps.row!.receiver" clearable></el-input>
      </el-form-item>
      <el-form-item label="联系方式" prop="contract">
        <el-input v-model="drawerProps.row!.contract" clearable></el-input>
      </el-form-item>
      <el-form-item label="收货地址" prop="address">
        <el-input v-model="drawerProps.row!.address" clearable></el-input>
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-input v-model="status" clearable></el-input>
      </el-form-item>
      <el-form-item label="买家" prop="buyer">
        <el-input v-model="drawerProps.row!.buyer" clearable></el-input>
      </el-form-item>
      <el-form-item label="支付方式" prop="payType">
        <el-input v-model="drawerProps.row!.payType" clearable></el-input>
      </el-form-item>
      <el-form-item label="送货方式" prop="deliveryType">
        <el-input v-model="drawerProps.row!.deliveryType" clearable></el-input>
      </el-form-item>
      <el-form-item label="下单时间" prop="createTime">
        <el-input v-model="drawerProps.row!.createTime" clearable></el-input>
      </el-form-item>
      <el-form-item label="付款时间" prop="payTime">
        <el-input v-model="drawerProps.row!.payTime" clearable></el-input>
      </el-form-item>
      <el-form-item label="发货时间" prop="deliveryTime">
        <el-input v-model="drawerProps.row!.deliveryTime" clearable></el-input>
      </el-form-item>
      <el-form-item label="收货时间" prop="confirmTime">
        <el-input v-model="drawerProps.row!.confirmTime" clearable></el-input>
      </el-form-item>
      <el-form-item label="完成时间" prop="completeTime">
        <el-input v-model="drawerProps.row!.completeTime" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="OrderDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { Order } from "@/api/interface";
import { getOrderStatus } from "@/api/modules/order";

const rules = reactive({
  orderNo: [{ required: true, message: "请填写订单号" }],
  amount: [{ required: true, message: "请填写金额" }],
  receiver: [{ required: true, message: "请填写收货人" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<Order.ResOrder>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const status = ref();
const statusList = ref();
const getStatusList = async () => {
  const { data } = await getOrderStatus();
  statusList.value = data;
  status.value = statusList.value[drawerProps.value.row.status ?? 0]?.label;
};

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
  getStatusList();
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success({ message: `${drawerProps.value.title}订单信息成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
